<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>课程表 - 拓扑排序算法详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 1px, transparent 1px);
            background-size: 50px 50px;
            animation: drift 20s linear infinite;
        }
        
        @keyframes drift {
            from { transform: translate(0, 0); }
            to { transform: translate(50px, 50px); }
        }
        
        .card-hover {
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            position: relative;
            overflow: hidden;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            display: flex;
            align-items: center;
            padding: 0 16px;
        }
        
        .code-content {
            padding: 60px 24px 24px;
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
            overflow-x: auto;
        }
        
        .keyword { color: #c586c0; }
        .function { color: #dcdcaa; }
        .string { color: #ce9178; }
        .number { color: #b5cea8; }
        .comment { color: #6a9955; }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 0.8;
            margin: 0.1em 0.1em 0.2em 0;
            font-weight: 700;
            color: #667eea;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .complexity-badge {
            display: inline-flex;
            align-items: center;
            padding: 0.5rem 1rem;
            background: rgba(102, 126, 234, 0.1);
            border: 1px solid rgba(102, 126, 234, 0.3);
            border-radius: 9999px;
            font-size: 0.875rem;
            font-weight: 500;
            color: #667eea;
            margin: 0.25rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white relative">
        <div class="container mx-auto px-6 py-24 relative z-10">
            <div class="max-w-4xl mx-auto text-center">
                <div class="mb-6">
                    <i class="fas fa-graduation-cap text-6xl opacity-80"></i>
                </div>
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                    课程表问题
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    探索拓扑排序的优雅解法，掌握依赖关系的处理艺术
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-clock mr-2"></i>中等难度
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-code mr-2"></i>图论算法
                    </span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-16 max-w-6xl">
        <!-- Problem Description -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12 card-hover">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                    <i class="fas fa-book-open mr-4 text-purple-600"></i>
                    题目描述
                </h2>
                <div class="prose prose-lg max-w-none text-gray-700">
                    <p class="leading-relaxed">
                        <span class="drop-cap">你</span>这个学期必须选修 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">numCourses</code> 门课程，课程编号从 0 到 numCourses-1。有的课程有先修课程，例如要学习课程 0，你必须先完成课程 1，用一个二元组 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">[0,1]</code> 表示（0 依赖 1）。
                    </p>
                    <p class="mt-4">
                        给定课程总数和先修课程要求，判断是否可能完成所有课程的学习。例如，输入 2 和 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">[[1,0]]</code> 表示总共 2 门课，学习课程 1 前需要完成课程 0，输出 <code class="bg-green-100 text-green-800 px-2 py-1 rounded">true</code>（可以先学 0，再学 1）。
                    </p>
                </div>
            </div>
        </section>

        <!-- Core Concepts -->
        <section class="mb-16">
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover border-t-4 border-purple-500">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-sitemap"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">拓扑排序</h3>
                    <p class="text-gray-600">有向无环图的顶点线性排序</p>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover border-t-4 border-blue-500">
                    <div class="text-blue-600 text-3xl mb-4">
                        <i class="fas fa-search"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">DFS 深度优先</h3>
                    <p class="text-gray-600">递归检测图中是否存在环</p>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover border-t-4 border-green-500">
                    <div class="text-green-600 text-3xl mb-4">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">BFS 广度优先</h3>
                    <p class="text-gray-600">层级遍历处理入度为零的节点</p>
                </div>
            </div>
        </section>

        <!-- Visual Representation -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                    <i class="fas fa-project-diagram mr-4 text-purple-600"></i>
                    算法可视化
                </h2>
                <div class="mermaid">
                    graph LR
                        A[课程0] --> B[课程1]
                        A --> C[课程2]
                        B --> D[课程3]
                        C --> D
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                </div>
                <p class="text-center text-gray-600 mt-4">
                    <i class="fas fa-info-circle mr-2"></i>
                    课程依赖关系示例：课程3需要先完成课程1和课程2
                </p>
            </div>
        </section>

        <!-- Solution Approaches -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 text-center">
                解题思路
            </h2>
            <div class="grid md:grid-cols-2 gap-8">
                <!-- DFS Approach -->
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-2xl p-8 border border-purple-200">
                    <h3 class="text-2xl font-bold mb-4 text-purple-800 flex items-center">
                        <i class="fas fa-code-branch mr-3"></i>
                        DFS 方法
                    </h3>
                    <p class="text-gray-700 mb-4">
                        检测图中是否有环，若无环则课程可完成。使用 DFS 检查每个节点，记录访问状态。
                    </p>
                    <div class="space-y-2">
                        <div class="complexity-badge">
                            <i class="fas fa-clock mr-2"></i>
                            时间复杂度：O(V + E)
                        </div>
                        <div class="complexity-badge">
                            <i class="fas fa-memory mr-2"></i>
                            空间复杂度：O(V)
                        </div>
                    </div>
                </div>

                <!-- BFS Approach -->
                <div class="bg-gradient-to-br from-blue-50 to-cyan-50 rounded-2xl p-8 border border-blue-200">
                    <h3 class="text-2xl font-bold mb-4 text-blue-800 flex items-center">
                        <i class="fas fa-layer-group mr-3"></i>
                        BFS 方法
                    </h3>
                    <p class="text-gray-700 mb-4">
                        使用拓扑排序，计算入度，从入度为 0 的节点开始，逐步删除边。
                    </p>
                    <div class="space-y-2">
                        <div class="complexity-badge">
                            <i class="fas fa-clock mr-2"></i>
                            时间复杂度：O(V + E)
                        </div>
                        <div class="complexity-badge">
                            <i class="fas fa-memory mr-2"></i>
                            空间复杂度：O(V)
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-xl overflow-hidden">
                <div class="bg-gradient-to-r from-purple-600 to-pink-600 text-white p-6">
                    <h2 class="text-2xl font-bold flex items-center">
                        <i class="fas fa-code mr-3"></i>
                        示例代码（BFS 法）
                    </h2>
                </div>
                <div class="code-block">
                    <div class="code-content">
<pre><span class="keyword">def</span> <span class="function">canFinish</span>(numCourses, prerequisites):
    <span class="keyword">from</span> collections <span class="keyword">import</span> defaultdict, deque
    
    <span class="comment"># 构建邻接表和入度数组</span>
    graph = defaultdict(list)
    indegree = [<span class="number">0</span>] * numCourses
    
    <span class="keyword">for</span> dest, src <span class="keyword">in</span> prerequisites:
        graph[src].append(dest)
        indegree[dest] += <span class="number">1</span>
    
    <span class="comment"># 将所有入度为0的节点加入队列</span>
    queue = deque([i <span class="keyword">for</span> i <span class="keyword">in</span> range(numCourses) <span class="keyword">if</span> indegree[i] == <span class="number">0</span>])
    count = <span class="number">0</span>
    
    <span class="comment">#